package com.somsoft.handjc.activity.tongji.xuncha;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.chart.SuperChart;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;

import com.somsoft.handjc.util.MyApplication;
import com.somsoft.handjc.util.Utils;
import com.somsoft.handjc.util.XunChaDataUtil;

/*
 * 业务量折线图
 */
@SuppressWarnings("all")
public class BusinessCountLineActivity extends SuperChart{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		super.onCreate(savedInstanceState);
//		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		MyApplication.getInstance().addActivity(this);
		try {
			
			JSONArray array = XunChaDataUtil.getBusinessCount(new ArrayList<NameValuePair>());
		
			String[] titles = new String[] { "业务总量", "完成数量" };
			
		    List<double[]> values = new ArrayList<double[]>();
		    double[] busiTotal = new double[array.length()];
		    double[] busiFinished = new double[array.length()];
		    double tmpMax = 0;
		    for(int i=0;i<array.length();i++){
		    	JSONObject obj = array.getJSONObject(i);
	    		busiTotal[i] = obj.getDouble("total");
	    		busiFinished[i] = obj.getDouble("finished");
	    		if(obj.getDouble("total")>tmpMax){
	    			tmpMax = obj.getDouble("total");
	    		}
		    }
		    values.add(busiTotal);
		    values.add(busiFinished);
		    int[] colors = new int[] { Color.rgb(77, 198, 244),Color.rgb(255, 102, 102) };
		    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
		    renderer.setOrientation(Orientation.HORIZONTAL);
		    setChartSettings(renderer, "业务量统计图", "", "", 0.5,
		        8.5, 0, tmpMax*1.1, Color.GRAY, Color.LTGRAY);
		    renderer.setXLabels(1);
		    renderer.setYLabels(10);
//		    renderer.setBarSpacing(0.2);
		   for(int i=0;i<array.length();i++){
			   JSONObject obj = array.getJSONObject(i);
			   String deptStr = obj.getString("name");
			   renderer.addXTextLabel(i+1, deptStr);
		   }
//		    int length = renderer.getSeriesRendererCount();
//		    for (int i = 0; i < length; i++) {
//		      SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
//		      seriesRenderer.setDisplayChartValues(true);
//		    }
//		    View bar = ChartFactory.getLineChartView(this, buildDataset(titles, x, values), renderer);
		   View bar = null;
		    setContentView(bar);
		    Utils.closeLoading();
		} catch (Exception e) {
			Log.e("BusinessCountLineActivity", e.getMessage(),e);
		}
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

}
